package com.ifengchao.util.fdfs;

import org.apache.log4j.Logger;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class FileManager implements FileManagerConfig {

	private static final long serialVersionUID = -7031772706821721183L;
	private static final Logger logger = Logger.getLogger(FileManager.class);
	private static TrackerClient trackerClient;
	private static TrackerServer trackerServer;
	private static StorageServer storageServer;
	private static StorageClient storageClient;
	
	public static String alias = ""; 
	public static String groupName = ""; 
	
	static {
		try {
			String classPath = new File(FileManager.class.getResource("/")
					.getFile()).getCanonicalPath();
			String fdfsClientConfigFilePath = classPath + File.separator
					+ CLIENT_CONFIG_FILE;
			ClientGlobal.init(fdfsClientConfigFilePath);

			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient(trackerServer, storageServer);
			Properties  props= PropertiesLoaderUtils.loadAllProperties(CLIENT_CONFIG_FILE);
			alias = props.getProperty("alias");
			groupName = props.getProperty("group_name");

		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	

	/**
	 * 上传文件到服务器
	 */
	public static String upload(FastDFSFile file) {
		logger.info("File Name: " + file.getName()	+ "	File Length: " + file.getContent().length);
		
		NameValuePair[] meta_list = new NameValuePair[3];
		meta_list[0] = new NameValuePair("fileName", file.getName());
		meta_list[1] = new NameValuePair("fileExtName", file.getExt());
		meta_list[2] = new NameValuePair("fileLength",String.valueOf(file.getContent().length));
		
		String[] uploadResults = null;
		try {
			uploadResults = storageClient.upload_file(groupName,file.getContent(),file.getExt(), meta_list);
		} catch (IOException e) {
			logger.error("IO Exception uploadind 文件时异常: " + file.getName(),e);
		} catch (Exception e) {
			logger.error("非IO Exception uploadind 文件时异常: " + file.getName(), e);
		}

		if (uploadResults == null) {
			//LoggerUtils.error(logger,"上传文件失败,错误代码: "+ storageClient.getErrorCode());
			return null;
		}

		String groupName = uploadResults[0];
		String remoteFileName = uploadResults[1];
		StringBuffer fileAbsolutePath = new StringBuffer();
		//LoggerUtils.info(logger,"文件信息 ：  group_name: " + groupName + ", remoteFileName: "	+ remoteFileName);

		if(null==alias || "null".equalsIgnoreCase(alias)){
			alias="";
		}
		fileAbsolutePath.append("".equals(alias)?"":SEPARATOR+alias);
		fileAbsolutePath.append(SEPARATOR + groupName).append(SEPARATOR + remoteFileName);
		return fileAbsolutePath.toString();

	}

	public static FileInfo getFile(String groupName, String remoteFileName) {

		try {
			return storageClient.get_file_info(groupName, remoteFileName);
		} catch (IOException e) {
			//LoggerUtils.error(logger,"IO Exception: 得到快速DFS文件失败", e);
		} catch (Exception e) {
			//LoggerUtils.error(logger,"非 IO Exception: 得到快速DFS文件失败 ", e);
		}
		return null;
	}
}
